﻿$(document).ready(function () {
    var editNamePopup;
    var data;
    var name;
    var nameExp;

    $('.editFile, .editFolder').click(function () {

        editNamePopup = $("#editnamepopup").bPopup({

            easing: 'easeOutBack',
            speed: 500,
            transition: 'slideDown',
            onClose: function () {
            }
        });


        data = $(this).attr("value").split(',');
        name;
        if (data[1] === ' file') {
            name = $(this).attr("name");
            nameExp = name.split('.');
            name = '';
            for (var i = 0; i < nameExp.length - 1; i++) {
                if (i < nameExp.length - 2)
                    name += nameExp[i] + '.';
                else name += nameExp[i];
            }
        }
        else name = $(this).attr("name");

        $('#editname input[type="text"]').val(name);
        $('#editname input[type="text"]').focus();
        $('#editname input[type="text"]').select();

    });


    $('#editOk').one('click', $('#editOk'), function () {
        if (typeof data != 'undefined' ) {
            var newName;            
            if (data[1] === ' file') {
                    newName = $('#editname input[type="text"]').val() + '.' + nameExp[nameExp.length - 1];
                } else newName = $('#editname input[type="text"]').val();

                $.ajax({
                    url: '/Edit/changeFileName',
                    type: 'POST',
                    data: { id: data[0], type: data[1], name: newName },
                    success: function () {
                        editNamePopup.close();
                        $.ajax({
                            url: '/Home/Index/' + $('.apriboxparentfolder').last().attr('id'),
                            dataType: 'html',
                            success: function (data) {
                                $('#main').html(data);
                            },
                            processData: false,
                            contentType: false,
                            cache: false
                        });
                    }
                });
             
        }
    });



    $('#editCancel').click( function () {

        editNamePopup.close();
    });

});

    



